{{define "/user/login.html"}}
<!DOCTYPE html>
<html>
<head>
    <title>IM-登录</title>
    {{template "/layout/head.html"}}
</head>
<body>

    <header class="mui-bar mui-bar-nav">
        <h1 class="mui-title">登录</h1>
    </header>
    <div class="mui-content" id="app">
        <form id='login-form' class="mui-input-group">
            <div class="mui-input-row">
                <label>账号</label>
                <input v-model="user.username" placeholder="手机号或邮箱" type="text" class="mui-input-clear mui-input">
            </div>
            <div class="mui-input-row">
                <label>密码</label>
                <input v-model="user.password" placeholder="请输入密码" type="password" class="mui-input-clear mui-input">
            </div>
        </form>
        <div class="mui-content-padded">
            <button @click="login" type="button" class="mui-btn mui-btn-block mui-btn-primary">登录</button>
            <div class="link-area"><a id='reg' href="register">注册账号</a> <span class="spliter">|</span> <a
                    id='forgetPassword'>忘记密码</a>
            </div>
        </div>
        <div class="mui-content-padded oauth-area">
        </div>
    </div>
</body>

</html>
<script>
    const app = Vue.createApp({
        data() {
            return {
                user: {
                    username: "",
                    password: ""
                },
            }
        },
        methods: {
            login: function () {
                if (this.user.username === '' || this.user.password === '') {
                    return mui.toast("账号或密码为空");
                }
                $.ajax({
                    url: "login",
                    type: "post",
                    data: {
                        username: this.user.username,
                        password: this.user.password
                    },
                    success: function (res) {
                        if (res.code !== 0) {
                            return mui.toast(res.msg);
                        } else {
                            mui.toast("登录成功!");
                            setTimeout(function(){
                                localStorage.setItem("token", res.data.token);
                                localStorage.setItem("userinfo", JSON.stringify(res.data.userinfo));
                                window.location.href = "home";
                            }, 500);
                        }
                    },
                    error: function (res) {
                        mui.toast("系统异常，请稍后再试");
                    }
                })
            },
        }
    })

    app.mount("#app")
</script>
{{end}}